package forms;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public class DegreeForm extends ActionForm{
	private static final long serialVersionUID = 1L;
	
	private String location;
	private String university;
	private int major;
	private String othermajor;
	private String month;
	private String year;
	private String gpa;
	
	
	public String getUniversity() {
		return university;
	}
	public void setUniversity(String university) {
		this.university = university;
	}
	
	public int getMajor() {
		return major;
	}
	public void setMajor(int major) {
		this.major = major;
	}
	public String getOthermajor() {
		return othermajor;
	}
	public void setOthermajor(String othermajor) {
		this.othermajor = othermajor;
	}
	public String getMonth() {
		return month;
	}
	public void setMonth(String month) {
		this.month = month;
	}
	public String getYear() {
		return year;
	}
	public void setYear(String year) {
		this.year = year;
	}
	public String getGpa() {
		return gpa;
	}
	public void setGpa(String gpa) {
		this.gpa = gpa;
	}
	
	public String getLocation() {
		return location;
	}
	public void setLocation(String location) {
		this.location = location;
	}
	
	public void reset(ActionMapping mapping, HttpServletRequest request) {
		setMajor(0);
		setOthermajor(null);
		setMonth(null);
		setYear(null);
		setGpa(null);
	}
	
	public static boolean isNumeric(String s)
	{
	   if((s != null)&&(s!=""))
	    return s.matches("^[0-9]*$");
	   else
	    return false;
	}
	
	public static boolean isDouble(String value) {
		try {
		   Double.parseDouble(value);
		   if (value.contains("."))
		    return true;
		   return false;
		} catch (NumberFormatException e) {
		   return false;
		}
		}
	
	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
		
		ActionErrors errors = new ActionErrors();
		
		if ((major==0) && (othermajor == null || othermajor.length()<1))
			errors.add("majorMsgTag1", new ActionMessage("errors.required",
					"Major"));

		if(!isNumeric(month)){
			errors.add("monthMsgTag1", new ActionMessage("errors.notanumber", "Month"));
		}
		
		if(!isNumeric(year)){
			errors.add("yearMsgTag1", new ActionMessage("errors.notanumber", "Year"));
		}
		
		if(!isDouble(gpa)){
			errors.add("gpaMsgTag1", new ActionMessage("errors.float", "GPA"));
		}		
		
		return errors;
	}
	
	
}
